//
//  HMSportGPSButton.m
//  BlackHorseLine
//
//  Created by made on 17/3/10.
//  Copyright © 2017年 made. All rights reserved.
//

#import "HMSportGPSButton.h"
#import "HMSportTracking.h"
#import "HMSportSportingVC.h"
#import "HMSportMapVC.h"

@implementation HMSportGPSButton

- (void)awakeFromNib
{
    [super awakeFromNib];
    
    //注册通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateUIWithGPSSingleState:) name:kHMSportGPSSignalStateChange object:nil];
}


- (void)dealloc
{
    //销毁通知
    [[NSNotificationCenter defaultCenter] removeObserver:self name:kHMSportGPSSignalStateChange object:nil];
}



- (void)updateUIWithGPSSingleState:(NSNotification *)notification
{
    NSLog(@"%@",self.nextResponder);
    NSLog(@"%@",self.nextResponder.nextResponder);
    //根据button所在响应链判断自己是在哪一个控制器上面
    id vc = self.nextResponder.nextResponder;
    //根据GPS信号强度来刷新GPS按钮
    //如果GPS信号没有发生变化则不刷新UI
    HMSportGPSSignalState state = [notification.userInfo[@"key"] integerValue];
    NSString *imageStr;
    if ([vc isMemberOfClass:[HMSportSportingVC class]]) {
        imageStr = @"ic_sport_gps_connect";
    }
    else
    {
        imageStr = @"ic_sport_gps_map_connect";
    }
    
    NSString *titleStr;
    
    if (state == HMSportGPSSignalStateDisconnect) {
        titleStr = @"  GPS已断开  ";
    }
    else if (state == HMSportGPSSignalStateStateBad)
    {
        titleStr = @"  建议绕开高楼大厦  ";
    }
    else
    {
        titleStr = nil;
    }
    
    [self setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@_%zd",imageStr,state-1]] forState:UIControlStateNormal];
    [self setTitle:titleStr forState:UIControlStateNormal];
    //更新布局
    [self layoutIfNeeded];
    //    //刷新按钮动画
    self.alpha = 0;
    [UIView animateWithDuration:0.5 animations:^
     {
         self.alpha = 1;
         
     }];
}





@end
